#!/usr/bin/env python3


import sys
from socket import socket, AF_INET, SOCK_STREAM
import time

class chatClient():
	def __init__(self, host, port):
		self.host, self.port = host, port
		self.sock = socket(AF_INET, SOCK_STREAM)
		self.sock.connect((self.host, self.port))
	
	def dispatch(self):
		while True:
			self.printMenu()
			key = input('select Menu :')
			reqMsg = self.launcherFunction(key)
			self.sock.send(reqMsg.encode())
			receiveBuf = self.sock.recv(1024).decode()
			print(receiveBuf)
			if key == 'q':
				self.sock.close()
				break;

	def printMenu(self):
		print("\nMovie Search Program")
		print("===========Menu===========")
		print("Quit program:	q")
		print("Get data from keyword: k")
		print("===========Menu===========")

	def launcherFunction(self, menu):
		keyValue = ''
		keyValue += menu
		if menu == 'q':
			print("end of Connection")
			return menu
		elif menu == 'k':
			keyword = input('input keyword to search :')
			keyValue += ' ' + keyword 
			return keyValue
		else :
			print("error : unknow menu key")

	
if __name__ == '__main__':
	if len(sys.argv) == 3:
		serverHost = sys.argv[1]
		serverPort = int(sys.argv[2])
	else:
		exit(1)

	client = chatClient(serverHost, serverPort)
	client.dispatch()
    
